#include "gc_ptr.h"
#include "taginfo.h"
extern std::map<void *, void*> mm;
void go (void *v, unsigned mark_bit)
{
	printf ("v = %p\n", v);
	if (v == 0)
		return;
	void *tmp;

	tmp = mm[v];

	printf ("go = %p\n", tmp);

	BLOCK_TAG* tag = (BLOCK_TAG *) tmp;
	tag->mbit = 1;

	tmp = tmp + sizeof(BLOCK_TAG);

	switch (tag->model) 
	{
		case 1:
			{
				//структура
				char n = *((char *)tmp);
				tmp = tmp + 1;
				printf ("%d %d\n", n, (*((POINTER_DESCR *)tmp)).offset);
				for (size_t i = 0; i < size_t(n); i++)
				{
				  printf ("%p %p\n", v, v + (*((POINTER_DESCR *)tmp)).offset / 8);
				  void *p = v + (*((POINTER_DESCR *)tmp)).offset / 8;
				
				  go(reinterpret_cast <void*> (*((unsigned long *)p)), mark_bit);
				  tmp = tmp + sizeof(POINTER_DESCR); 
				}
			}
			break;
		case 2:
			//простой тип
			break;
		case 3:
			//массив указателей
			for (size_t i = 0; i < tag->size; i++)
				go (v + i * sizeof(void*), mark_bit);
			break;
		case 4:
			//массив
			break;
		default:
			printf ("Unknown model!\n");	
			break;
	}
}

void mark ()
{
	for (ptr_list* root = collect(); root != 0; root = root->next)
		go (root, 1);

	//All available memory cells are marked in 1

	for (ptr_list* root = collect(); root != 0; root = root->next)
		go (root, 0);
}

